{% extends "shuup/customer_information/macros/theme/address.jinja" %}

{% from "shuup/front/macros/general.jinja" import render_field %}

{% macro render_address(addr) %}
    {% set region = (addr.region_code or addr.region) %}
    <p><b>{% trans %}Name{% endtrans %}:</b> {{ addr.name }}</p>
    <p><b>{% trans %}Email{% endtrans %}:</b> {{ addr.email or _("Not specified") }}</p>
    <p><b>{% trans %}Phone{% endtrans %}:</b> {{ addr.phone or _("Not specified") }}</p>
    {% if addr.postal_code and addr.city and region %}
        <p><b>{% trans %}Address{% endtrans %}:</b><br>{{ addr.street }}</p>
        <p>{{ addr.postal_code }} {{ addr.city }}, {{ region }}</p>
    {% else %}
        <p><b>{% trans %}Street{% endtrans %}:</b> {{ addr.street }}</p>
        <p><b>{% trans %}Postal code{% endtrans %}:</b> {{ addr.postal_code or _("Not specified") }}</p>
        <p><b>{% trans %}City{% endtrans %}:</b> {{ addr.city }}{% if region %}, {{ region }}{% endif %}</p>
        <p><b>{% trans %}Country{% endtrans %}:</b> {{ addr.country.name }}</p>
    {% endif %}
{% endmacro %}

{% macro render_address_forms(billing_form, shipping_form) %}
    {% for field in billing_form.hidden_fields() %}{{ field|safe }}{% endfor %}
    {% for field in shipping_form.hidden_fields() %}{{ field|safe }}{% endfor %}
    <div class="row">
        <div class="col-md-6">
            <fieldset>
                <legend>{% trans %}Billing information{% endtrans %}</legend>
                {% for field in billing_form.visible_fields() %}
                    {{ render_field(field, show_help_text=False) }}
                {% endfor %}
            </fieldset>
            <button class="btn btn-primary btn-copy-address" type="button" onclick="copyAddress('billing', 'shipping')">
                {% trans %}Copy to shipping information{% endtrans %}
            </button>
        </div>
        <div class="col-md-6">
            <fieldset>
                <legend>{% trans %}Shipping information{% endtrans %}</legend>
                {% for field in shipping_form.visible_fields() %}
                    {{ render_field(field, show_help_text=False) }}
                {% endfor %}
            </fieldset>
            <button class="btn btn-primary btn-copy-address" type="button" onclick="copyAddress('shipping', 'billing')">
                {% trans %}Copy to billing information{% endtrans %}
            </button>
        </div>
    </div>
{% endmacro %}

{% macro render_address_forms_js() %}
    <script>
        function copyFields(from_fields, to_fields) {
            $("[id^=id_" + from_fields + "]").each(function() {
                var targetName = $(this).attr("id").split("-")[1];
                var $target = $("#id_" + to_fields + "-" + targetName);
                $target.val($(this).val());
            });
        }
        function copyAddress(from_fields, to_fields) {
            // copy values
            copyFields(from_fields, to_fields);
            // trigger field change event so country and regions can be populated when regions app is enabled
            $("[id^=id_" + to_fields + "]").trigger("change");
            // copy values again as regions select can be changed after the first copy
            copyFields(from_fields, to_fields);
        }
    </script>
{% endmacro %}
